﻿
using System;
using System.Collections.Concurrent;
using Test.EventBus2.Core;
using Test.EventBus2.Message.Events.Orders;
using Test.EventBus2.Order.Services;

namespace Test.EventBus2.Order.Services
{
    public class OrderService : IOrderService
    {
        private ConcurrentDictionary<string, OrderInfo> orderlist = new ConcurrentDictionary<string, OrderInfo>();
        private IBus _bus;
        public OrderService(IBus bus)
        {
            _bus = bus;
        }
        public bool CreateOrder(OrderInfo orderInfo)
        {
            orderlist.AddOrUpdate(orderInfo.Id.ToString(), orderInfo, (key, value) =>
            {
                return value;
            });
            _bus.PublishMessage(new OrderCreationEvent()
            {
                ProductName = orderInfo.Name,
                ProductId = Guid.NewGuid()
            });
            return true;

        }
    }
}
